package org.jtzc.langchang4jdemo;

import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;

/**
 * @author wu chuang
 * @description
 */
public class Demo3 {
    public static void main(String[] args) throws InterruptedException {

//        StreamingChatLanguageModel model = OpenAiStreamingChatModel.builder()
//                .apiKey(System.getenv("OPENAI_API_KEY"))
//                .modelName("gpt-4o-mini")
//                .build();

//        StreamingChatLanguageModel model = OpenAiStreamingChatModel.builder()
//                .apiKey(System.getenv("OPENAI_API_KEY"))
//                .defaultRequestParameters(OpenAiChatRequestParameters.builder()
//                        .modelName("gpt-4o-mini")
//                        .build())
//                .build();

        StreamingChatLanguageModel model = OpenAiStreamingChatModel.builder()
                .baseUrl("https://yunwu.ai/v1")
                .apiKey("sk-o4cHAbfbUSVXpgPS1yIC2G5U1tAflsPuji7GfFCIcgqSK0Xn") //设置模型apiKey
                .modelName("gpt-4o-mini") //设置模型名称
                .build();
        model.chat("你好？", new StreamingChatResponseHandler() {
            @Override
            public void onPartialResponse(String s) {
                System.out.println("Partial Response: " + s);
                try {
                    Thread.sleep(100); // 模拟一些处理延迟
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onCompleteResponse(ChatResponse chatResponse) {
                System.out.println("Complete Response: " + chatResponse.aiMessage().text());
                System.out.println("onCompleteResponse");
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("onError: " + throwable.getMessage());
                throwable.printStackTrace(); // 打印堆栈跟踪以获取更多错误信息
            }
        });


        Thread.sleep(10000000);


    }

}
